package coop.bancocredicoop.proyectos.gd

/**
 *
 */
class DocumentServiceTest extends GroovyTestCase {

    def documentService

    def private aDocumentFile() {
        
        [getInputStream: {
            new ByteArrayInputStream(
                    "testing document content".getBytes("UTF-8"));
         },
         getOriginalFilename: {
            "testingFileName"
         },
         delete: {
         }
        ]        

    }
    
    def private aDocumentFileType() {
        def fileType = new FileType(shortName: 'ALTA', name: 'Alta')
        fileType.save()
        fileType
    }

    def private aDocumentType(fileType) {
        def data1 = [shortName: 'doctypetest',
                     name: 'Doc Type Test',
                     versionable: true,
                     purgeable: true,
                     hasExpiration: true,
                     daysInAdvanceForNotice: 14,
                     defaultLastingMonths: 12,
                     uploadNotificationEmailAddress: null,
                     fileType: fileType]
        def docType = new DocumentType(data1)
        docType.save()
        docType
    }   

    def private aCustomer() {
        def customer = new Customer(idPersona: 12345,
                                    cuit: '20-33333333-1',
                                    name: 'Testing Customer')
        customer.save()
        customer
    } 

    void testSomething() {
        assertTrue true
    }

    void _testSomething() {
        // TODO: GORM + H2 database gives an error when using to_char function
        // in Select to convert date type fields.

        def fileType = aDocumentFileType() 
        def documentType = aDocumentType(fileType)
        def customer = aCustomer()
        
        def customerId = customer.idPersona
        def box = '12345'
        def expirationDate = new Date(2015, 1, 1)
        def tags = []
        def documentFile = aDocumentFile()
        def loggedInUser = 'testuser'
        def now = new Date(2014, 1, 1)
        def notes = 'test notes'
       
        def result = documentService.createDocument(
                                       customerId, 
                                       box, 
                                       documentType.id.toString(), 
                                       expirationDate, 
                                       tags, 
                                       documentFile,
                                       loggedInUser,
                                       now, 
                                       notes)
        assertTrue result.sucess 
    }
}
